package com.mobileares.midp.widgets.client.tree;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TreeImages;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-3-22
 * Time: 15:42:36
 * To change this template use File | Settings | File Templates.
 */
public class CTree extends Composite implements HasSelectionHandlers<CTreeItem> {
    private List<CTreeItem> roots = new ArrayList<CTreeItem>();

    /**
     * Adds a {@link com.google.gwt.event.logical.shared.SelectionEvent} handler.
     *
     * @param handler the handler
     * @return the registration for the event
     */
    public HandlerRegistration addSelectionHandler(SelectionHandler<CTreeItem> handler) {
        return addHandler(handler, SelectionEvent.getType());
    }

    static class ImageAdapter {
        private static final CTreeResource DEFAULT_RESOURCES = GWT.create(CTreeResource.class);
        private final AbstractImagePrototype treeClosed;
        private final AbstractImagePrototype treeLeaf;
        private final AbstractImagePrototype treeOpen;

        public ImageAdapter() {
            this(DEFAULT_RESOURCES);
        }

        public ImageAdapter(CTreeResource resources) {
            treeClosed = AbstractImagePrototype.create(resources.treeClosed());
            treeLeaf = AbstractImagePrototype.create(resources.treeLeaf());
            treeOpen = AbstractImagePrototype.create(resources.treeOpen());
        }

        public ImageAdapter(TreeImages images) {
            treeClosed = images.treeClosed();
            treeLeaf = images.treeLeaf();
            treeOpen = images.treeOpen();
        }

        public AbstractImagePrototype treeClosed() {
            return treeClosed;
        }

        public AbstractImagePrototype treeLeaf() {
            return treeLeaf;
        }

        public AbstractImagePrototype treeOpen() {
            return treeOpen;
        }
    }

    private FlowPanel container;
    private CTreeItem selected;


    public CTree() {
        container = new FlowPanel();
        container.setStyleName("tree-ctree");
        initWidget(container);
    }

    public void addTreeItem(CTreeItem item) {
        container.add(item);
        item.setTree(this);
        roots.add(item);

    }

    public Iterator<CTreeItem> getIterator() {
        return roots.iterator();
    }

    public void expandAll() {
        Iterator it = getIterator();
        for (; it.hasNext();) {
            CTreeItem item = (CTreeItem) it.next();
            item.open();
        }
    }

    public void closedAll() {
        Iterator it = getIterator();
        for (; it.hasNext();) {
            CTreeItem item = (CTreeItem) it.next();
            item.close();
        }
    }

    public void clear() {
        selected = null;
        container.clear();
    }

    void openTreeItem(CTreeItem item) {
        int index = container.getWidgetIndex(item);
   
            for (CTreeItem child : item.getChildren()) {
                if (child.isFire()) {
                    child.setVisible(true);
                } else {
                    child.setTree(this);
                    child.setPadding();
                    container.insert(child, ++index);
                    child.setFire(true);
                }

            }
    }

    void closeTreeItem(CTreeItem item) {
        if (item.getChildren() != null)
            for (CTreeItem child : item.getChildren()) {
                if (child.isFire())
                    child.setVisible(false);
            }
    }

    public CTreeItem getSelected() {
        return selected;
    }

    void setSelected(CTreeItem selected) {
        this.selected = selected;
    }
}
